package StudyCode.summary.mycollection;

import java.util.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CombineToMapValues {
	public static Map combine(Map beCombineMap, Map toCombineMap) {
		List<Map> p1 = new ArrayList<Map>(128);
		List<Map> p2 = new ArrayList<Map>(128);
		{
			p1.add(beCombineMap);
			p2.add(toCombineMap);
		}
		while (!p2.isEmpty()) {
			Map map1 = p1.remove(p1.size() - 1);
			Map map2 = p2.remove(p2.size() - 1);
			for (Map.Entry entry : (Set<Map.Entry>) map2.entrySet()) {
				if (entry.getValue() instanceof Map) {
					Object object = map1.get(entry.getKey());
					if (object == null) {
						object = new LinkedHashMap();
						map1.put(entry.getKey(), object);
						{
							p1.add((Map) object);
							p2.add((Map) entry.getValue());
						}
					} else if (!(object instanceof Map)) {
						object = new LinkedHashMap();
						map1.put(entry.getKey(), object);
						{
							p1.add((Map) object);
							p2.add((Map) entry.getValue());
						}
					}
				} else {
					map1.put(entry.getKey(), entry.getValue());
				}
			}
		}
		return beCombineMap;
	}
}
